package com.huihui.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.huihui.MainApp;
import com.huihui.R;
import com.huihui.activity.login.LoginActivity;
import com.huihui.base.BaseActivity;
import com.huihui.datamanager.AppManager;
import com.huihui.eventbus.TestEvent;
import com.huihui.eventbus.TokenNoUseEvent;
import com.huihui.fragment.main.HomeFragment;
import com.huihui.fragment.main.MineFragment;
import com.huihui.utils.IntentUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 主界面
 */
public class MainActivity extends BaseActivity {

    private LinearLayout home_ll;
    private ImageView home_iv;
    private LinearLayout mine_ll;
    private ImageView mine_iv;
    private ImageView add;


    private HomeFragment homeFragment;
    private MineFragment mineFragment;
    private FragmentManager fm;

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.CAMERA",
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        initPermission();
        initView();
        initListener();
    }

    private void initPermission() {
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(MainActivity.this,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        home_ll = (LinearLayout) findViewById(R.id.home_ll);
        home_iv = (ImageView) findViewById(R.id.home_iv);
        mine_ll = (LinearLayout) findViewById(R.id.mine_ll);
        mine_iv = (ImageView) findViewById(R.id.mine_iv);
        add = (ImageView) findViewById(R.id.add);
        fm = getSupportFragmentManager();
        setTabSelection(0);
    }

    private void initListener() {
        home_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换主页
                setTabSelection(0);
            }
        });
        mine_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换我的
                setTabSelection(1);
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发表
                IntentUtils.gotoPublicActivity(getContext());
            }
        });

    }


    private void setTabSelection(int index){
        if(index == 0){
            home_iv.setSelected(true);
            mine_iv.setSelected(false);
        }else{
            home_iv.setSelected(false);
            mine_iv.setSelected(true);
        }
        FragmentTransaction ft = fm.beginTransaction();
        hideFragment(ft);
        switch (index) {
            case 0:
                if(homeFragment==null){
                    homeFragment = new HomeFragment();
                    ft.add(R.id.content, homeFragment);
                }else{
                    ft.show(homeFragment);
                }

                break;

            case 1:
                if(mineFragment==null){
                    mineFragment = new MineFragment();
                    ft.add(R.id.content, mineFragment);
                }
                ft.show(mineFragment);
                break;
        }
        ft.commit();
    }

    //用于隐藏fragment
    private void hideFragment(FragmentTransaction ft){
        if(homeFragment!=null){
            ft.hide(homeFragment);
        }if(mineFragment!=null){
            ft.hide(mineFragment);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(TestEvent messageEvent) {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(TokenNoUseEvent tokenNoUseEvent) {
        if(tokenNoUseEvent != null){
            Log.d(TAG,"清除token...");
            AppManager.setUserInfo(null);
            AppManager.setToken(null);
            Log.d(TAG,"清除token完毕");
            Intent intent = new Intent(getContext(),LoginActivity.class);
            getContext().startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    private void autoLogin(){

    }

    @Override
    protected void onResume() {
        super.onResume();
        hideInput(this);
    }
}
